package com.wisemoresystem.seal.action_app.install;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_user;
import com.wisemoresystem.seal.model.t_seal_user_use;
import com.wisemoresystem.seal.service.SealSetService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * .net调用获取印控仪,印章json
 */

@Controller("ixNetInstallAction")
@Scope("prototype")
@RequestMapping("/ix/net/install")
public class InstallAction extends ProjBaseAction {

	@Autowired
	private SealSetService sealService;

	/**
	 * 印章卸载列表
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/list")
	@ResponseBody
	public String list() throws IOException {

		List<Map<String, Object>> list = sealService.selectSealInstallList(
				null, page);

		String info = JsonTools.toJson(list);
		return info;
	}

	// @RequestMapping("/chk_seal")
	// @ResponseBody
	// public String chk_seal(){
	// List<Map<String,Object>> list = sealService.selectSealDetail(null);
	// return null;
	// }
	/**
	 * 印章详细信息
	 * 
	 * @param seal_id
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String seal_id) throws IOException {

		List<Map<String, Object>> maclist = sealService.selectSealMacList();
		String info = JsonTools.toJson(maclist);

		return info;
	}

	/**
	 * 印章安装卸载
	 * 
	 * @param seal_use
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public String edit(t_seal_user_use seal_use) {

		t_seal_user_use user_user = sealService.selectGetSealUserUse(seal_use
				.getSeal_id());
		if (ChkTools.isNotNull(user_user)) {
			return "yet";
		} else {
			t_seal_user seal_user = sealService.selectGetSealUser(seal_use
					.getSeal_id());
			t_seal seal = oc.findObjById(seal_use.getSeal_id(), t_seal.class);
			seal_use.setBank_id(WebTools.getCurrentBankId());
			seal_use.setTime_in(new Timestamp(System.currentTimeMillis()));
			seal_use.setUser_id(WebTools.getCurrentUserId());
			seal_use.setSeal_user_id(seal_user.get_id());
			seal_use.setUse_user_id(seal_user.getUse_user_id());
			seal_use.setFlag("已安装");
			oc.saveObj(seal_use);

			seal_user.setFlag("使用中");
			oc.updateObj(seal_user);
			seal.setMac_id(seal_use.getMac_id());
			seal.setFlag_use("使用中");
			seal.setFlag_use_status("已安装");
			oc.updateObj(seal);
			logT.logH("印章安装", t_seal_user.class, seal_user.get_id());
			return "true";
		}

	}

	/**
	 * 印章卸载
	 * 
	 * @param seal_id
	 * @return
	 */
	@RequestMapping("/operate")
	@ResponseBody
	public String operate(String seal_id) {

		t_seal_user_use user_user = sealService.selectGetSealUserUse(seal_id);
		if (ChkTools.isNull(user_user)) {
			return "yet";
		} else {
			t_seal_user_use seal_use = sealService
					.selectGetSealUserUse(seal_id);
			seal_use.setTime_out(new Timestamp(System.currentTimeMillis()));
			seal_use.setFlag("已卸载");
			oc.updateObj(seal_use);

			t_seal_user seal_user = oc.findObjById(seal_use.getSeal_user_id(),
					t_seal_user.class);
			t_seal sealDb = oc.findObjById(seal_id, t_seal.class);
			sealDb.setFlag_use_status("未安装");
			if ("机构已改动，请处理印章".equals(sealDb.getFlag_change())) {
				sealDb.setFlag_use("待归还");
			} else {
				sealDb.setFlag_use("已领取");
			}
			oc.updateObj(sealDb);

			seal_user.setFlag("已领取");
			oc.updateObj(seal_user);

			logT.logH("印章卸载", t_seal_user.class, seal_user.get_id());
			return "true";
		}
	}

}
